package com.itheima;

import com.alibaba.druid.pool.DruidDataSource;
import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.TypeAliasRegistry;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.List;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-14 11:25
 * @description 测试使用mybatis是否正常!!!!
 */
public class App04 {
    public static void main(String[] args) {
        //创建 sqlSessionFactoryBuilder 底层创建者模式
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        //创建sqlSessionFactory 由于需要使用sqlSessionFactoryBuilder创建

        InputStream inputStream = App04.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");


        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

        /*修改成自己的数据库运行环境 不用内置那个 内置那个性能差一点*/

        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("123");
        dataSource.setUrl("jdbc:mysql://localhost:3306/heima32_spring?characterEncoding=utf-8");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");

        Environment environment = new Environment("我自己的环境", new JdbcTransactionFactory(),dataSource);
        sqlSessionFactory.getConfiguration().setEnvironment(environment);

//        TypeAliasRegistry aliasRegistry = sqlSessionFactory.getConfiguration().getTypeAliasRegistry();
//        aliasRegistry.registerAlias("account",Account.class);


        //需要sqlSession

        SqlSession sqlSession = sqlSessionFactory.openSession(false);


        //执行逻辑
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);

        List<Account> all = accountDao.findAll();

        System.out.println(all);


        sqlSession.commit();


        //关闭资源

        sqlSession.close();








    }
}
